package com.lkd.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.dao.ChannelDao;
import com.lkd.emq.MqttProducer;
import com.lkd.entity.ChannelEntity;
import com.lkd.vo.TianxingDataVO;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class SupplyMsgJob {

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private MqttProducer mqttProducer;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 每天凌晨定时查询需要补货的售货机和货道信息
     */
    @XxlJob("getSupplyVmInfo")
    public void getSupplyVmInfo(){


        List<ChannelEntity> channelsNeedToSupply = channelDao.getChannelsNeedToSupply(XxlJobHelper.getShardTotal(),
                XxlJobHelper.getShardIndex());

        //如果集合没有数据
        if(CollectionUtils.isEmpty(channelsNeedToSupply)){
            return;
        }

        LinkedMultiValueMap<String,ChannelEntity> map =
                new LinkedMultiValueMap();

        for (ChannelEntity channel : channelsNeedToSupply) {
            map.add(channel.getInnerCode(),channel);
        }

        //遍历每个售货机
        map.entrySet().forEach(data -> {
            SupplyContract supplyContract = new SupplyContract();
            supplyContract.setInnerCode(data.getKey()); //售货机编号

            List<SupplyChannel> SupplyChannels = data.getValue().stream()
                    .map(channel -> {
                        SupplyChannel supplyChannel = new SupplyChannel();
                        supplyChannel.setChannelId(channel.getChannelCode());
                        supplyChannel.setCapacity(channel.getMaxCapacity() - channel.getCurrentCapacity());
                        supplyChannel.setSkuId(channel.getSkuId());
                        supplyChannel.setSkuImage(channel.getSku().getSkuImage());
                        supplyChannel.setSkuName(channel.getSku().getSkuName());
                        return supplyChannel;
                    }).collect(Collectors.toList());

            supplyContract.setSupplyData(SupplyChannels);

            //发送消息
            try {
                mqttProducer.send(TopicConfig.TASK_SUPPLY_TOPIC,2,supplyContract);
            } catch (JsonProcessingException e) {
                log.error("发送定时任务补货消息出错，json序列化错误");
            }
        });

//        Map<String, List<ChannelEntity>> maps = new HashMap<>();
//
//        for (ChannelEntity channel : channelsNeedToSupply) {
//            if(!maps.containsKey(channel.getInnerCode())){
//                //如果不包含key
//                ArrayList<ChannelEntity> channelEntities = new ArrayList<>();
//                maps.put(channel.getInnerCode(),channelEntities);
//            }
//
//            maps.get(channel.getInnerCode()).add(channel);
//        }


//        //获取的就是所有的售货机编码
//        List<String> innerCodes = channelsNeedToSupply.stream().
//                map(ChannelEntity::getInnerCode).distinct().collect(Collectors.toList());
//
//        //遍历售货机编码
//        for (String innerCode : innerCodes) {
//            List<ChannelEntity> list = channelsNeedToSupply.stream().filter(data -> data.getInnerCode().equals(innerCode))
//                    .collect(Collectors.toList());
//        }


    }
}
